package com.yanqu.road.dao.impl.activity.dunhuangshiku;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuServerUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/7/20 15:15
 */
public class DunHuangShiKuServerUserDataDaoImpl extends TempDataObjectDao<DunHuangShiKuServerUserData> {

    public static String getDunHuangShiKuServerUserDataSql = "select * from `t_u_activity_dunhuangshiku_user_data` where `activityId`=? and `userId`=?";
    public DunHuangShiKuServerUserData getDunHuangShiKuServerUserData(long activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDunHuangShiKuServerUserDataSql);
            int i = 1;
            call.setLong(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateDunHuangShiKuServerUserDataSql = "update `t_u_activity_dunhuangshiku_user_data` set touchMallMap=?,boxKeyNum=?," +
            "touchMallTimesData=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(DunHuangShiKuServerUserData dunHuangShiKuServerUserData) {
        dunHuangShiKuServerUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, dunHuangShiKuServerUserData.getTouchMallMapJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dunHuangShiKuServerUserData.getBoxKeyNumMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, dunHuangShiKuServerUserData.getTouchMallTimesMapJsonStr()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuServerUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuServerUserData.getUserId()));
        if (getDb().executeNoneQuery(updateDunHuangShiKuServerUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDunHuangShiKuServerUserDataSql = "insert into `t_u_activity_dunhuangshiku_user_data`(activityId,userId,touchMallMap," +
            "boxKeyNum,touchMallTimesData) values (?,?,?,?,?)";
    @Override
    protected boolean addData(DunHuangShiKuServerUserData dunHuangShiKuServerUserData) {
        dunHuangShiKuServerUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuServerUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, dunHuangShiKuServerUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, dunHuangShiKuServerUserData.getTouchMallMapJsonStr()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(dunHuangShiKuServerUserData.getBoxKeyNumMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, dunHuangShiKuServerUserData.getTouchMallTimesMapJsonStr()));
        if (getDb().executeNoneQuery(insertDunHuangShiKuServerUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DunHuangShiKuServerUserData parseData(ResultSet rs) throws SQLException {
        DunHuangShiKuServerUserData dunHuangShiKuServerUserData = new DunHuangShiKuServerUserData();
        dunHuangShiKuServerUserData.setActivityId(rs.getLong("activityId"));
        dunHuangShiKuServerUserData.setUserId(rs.getLong("userId"));
        dunHuangShiKuServerUserData.setTouchMallMap(JSON.parseObject(rs.getString("touchMallMap"), new TypeReference<java.util.Map<Integer, Integer>>() {}));
        if (!StringUtils.isNullOrEmpty(rs.getString("boxKeyNum"))) {
            dunHuangShiKuServerUserData.setBoxKeyNumMap(JSON.parseObject(rs.getString("boxKeyNum"), new TypeReference<java.util.Map<Integer, Long>>() {}));
        }
        String param = rs.getString("touchMallTimesData");
        if(StringUtils.isNullOrEmpty(param)){
            param = "{}";
        }
        dunHuangShiKuServerUserData.setTouchMallTimesMap(JSON.parseObject(param, new TypeReference<java.util.Map<Integer, Integer>>() {}));
        return dunHuangShiKuServerUserData;
    }

}
